/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.checker.type;


/**
 * A primitive integer type.
 */
final public class IntegerType extends Type {
	
	
	/**
	 * Signed 8-bit integer type.  Same as Java's <code>byte</code>.
	 */
	final public static IntegerType SIGNED_8 = new IntegerType(8, true);


	/**
	 * Signed 16-bit integer type.  Same as Java's <code>short</code>.
	 */
	final public static IntegerType SIGNED_16 = new IntegerType(16, true);
	

	/**
	 * Signed 32-bit integer type.  Same as Java's <code>int</code>.
	 */
	final public static IntegerType SIGNED_32 = new IntegerType(32, true);


	/**
	 * Signed 64-bit integer type.  Same as Java's <code>long</code>.
	 */
	final public static IntegerType SIGNED_64 = new IntegerType(64, true);
	

	/**
	 * Unsigned 8-bit integer type.
	 */
	final public static IntegerType UNSIGNED_8 = new IntegerType(8, true);


	/**
	 * Unsigned 16-bit integer type.  Same as Java's <code>char</code>.
	 */
	final public static IntegerType UNSIGNED_16 = new IntegerType(16, true);


	/**
	 * Unsigned 32-bit integer type.
	 */
	final public static IntegerType UNSIGNED_32 = new IntegerType(32, true);

	
	/**
	 * Unsigned 64-bit integer type.
	 */
	final public static IntegerType UNSIGNED_64 = new IntegerType(64, true);


	/**
	 * Width in bits of this integer type.
	 */
	private int width;
	
	
	/**
	 * True if this integer type is signed, false otherwise.
	 */
	private boolean signed;

	
	/**
	 * Constructs a new integer type.  Private to enforce the singleton pattern.
	 * 
	 * @param width  the width of the type in bits.
	 * @param signed  true if this type is signed, false otherwise
	 */
	private IntegerType(int width, boolean signed) {
		super();
		this.width = width;
		this.signed = signed;
	}


	/**
	 * Returns true since primitive types are always local.
	 */
	public boolean isLocal() {
		return true;
	}


	/**
	 * Returns false, since integer types are primitive.
	 * 
	 * @return false
	 */
	@Override
	public boolean isCompound() {
		return false;
	}
	
	
	/**
	 * Returns the width of this type in bits.
	 * 
	 * @return  this type's width
	 */
	public int getWidth() {
		return width;
	}
	
	
	/**
	 * Returns true if this type is signed.
	 * 
	 * @return true if this type is signed, false if this type is unsigned.
	 */
	public boolean isSigned() {
		return signed;
	}

	
	/**
	 * Returns the desired integer type.
	 * 
	 * @param signed  true if the returned type should be signed, false if the returned
	 *   type should be unsigned
	 * @param width  the width of the desired type in bits; must be 8, 16, 32 or 64
	 * @return  the integer type with that width and signage
	 */
	public static IntegerType get(boolean signed, int width) {
		if (signed) {
			switch (width) {
				case 8:
					return SIGNED_8;
				case 16:
					return SIGNED_16;
				case 32:
					return SIGNED_32;
				case 64:
					return SIGNED_64;
				default:
					throw new IllegalArgumentException(width + " is not a valid width.");
			}
		} else {
			switch (width) {
				case 8:
					return UNSIGNED_8;
				case 16:
					return UNSIGNED_16;
				case 32:
					return UNSIGNED_32;
				case 64:
					return UNSIGNED_64;
				default: 
					throw new IllegalArgumentException(width + " is not a valid width.");
			}
		}
	}


	@Override
	public boolean canAssignTo(Type formal) {
		if (!(formal instanceof IntegerType)) {
			return false;
		}
		IntegerType it = (IntegerType)formal;
		return it.width >= width;
	}


	@Override
	public String toString() {
		String base;
		switch (width) {
			case 8:
				base = "byte";
				break;
			case 16:
				base = "char";
				break;
			case 32:
				base = "int";
				break;
			case 64:
				base = "long";
				break;
			default:
				throw new IllegalStateException();
		}
		if (signed) {
			return base;
		} else {
			return "u" + base;
		}
	}

	
	public static int getMinimumBitWidth(boolean signed, long value) {
		if (signed) {
			if (value < 0) {
				if (value > Byte.MIN_VALUE) {
					return 8;
				} else if (value > Short.MIN_VALUE) {
					return 16;
				} else if (value > Integer.MIN_VALUE) {
					return 32;
				} else {
					return 64;
				}
			} else {
				if (value < Byte.MAX_VALUE) {
					return 8;
				} else if (value < Short.MAX_VALUE) {
					return 16;
				} else if (value < Integer.MAX_VALUE) {
					return 32;
				} else {
					return 64;
				}				
			}
		} else { // unsigned
			if (value < Byte.MAX_VALUE * 2 + 1) {
				return 8;
			} else if (value < Short.MAX_VALUE * 2 + 1) {
				return 16;
			} else if (value < (long)Integer.MAX_VALUE * 2L + 1L) {
				return 32;
			} else {
				return 64;
			}				
		}
	}

}
